package cn.iocoder.yudao.module.member.service.recruitCompany;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.job.vo.MemberJobPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.recruitCompany.vo.*;
import cn.iocoder.yudao.module.member.convert.recruitCompany.RecruitCompanyConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.MemberJobDO;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.DistributionDo;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.JobAndInvitCountDo;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.JobPassDo;
import cn.iocoder.yudao.module.member.dal.dataobject.recruitCompany.RecruitCompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.member.dal.mysql.company.CompanyMapper;
import cn.iocoder.yudao.module.member.dal.mysql.job.MemberJobMapper;
import cn.iocoder.yudao.module.member.dal.mysql.recruitCompany.RecruitCompanyMapper;
import cn.iocoder.yudao.module.member.dal.mysql.student.StudentMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.COMPANY_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.RECRUIT_COMPANY_NOT_EXISTS;

/**
 * 招聘企业管理 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class RecruitCompanyServiceImpl implements RecruitCompanyService {

    @Resource
    private RecruitCompanyMapper recruitCompanyMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private MemberJobMapper memberJobMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public PageResult<RecruitCompanyDO> getRecruitCompanyPage(RecruitCompanyPageReqVO recruitCompanyPageReqVO)
    {
        Integer total = recruitCompanyMapper.countRecruitCompany(recruitCompanyPageReqVO);
        if (total <= 0) {
            return PageResult.empty();
        }
        //用于导出
        if(StringUtils.isNotBlank(recruitCompanyPageReqVO.getExportFlag()) && "1".equals(recruitCompanyPageReqVO.getExportFlag())){
            recruitCompanyPageReqVO.setPageSize(total+10);
        }
        List<RecruitCompanyDO> recruitCompanyPage=recruitCompanyMapper.selectrecruitCompanyPage(recruitCompanyPageReqVO);
        return PageResult.build(recruitCompanyPage, total);
    }



    @Override
    public List<CompanyDO> getNoShowCompanyList() {
        return recruitCompanyMapper.getNoShowCompanyList();
    }

    @Override
    public void setCompanyStatus(RecruCompanyUpdateReqVO recruCompanyUpdateReqVO) {
        // 校验存在
        this.validateCompanyExists(recruCompanyUpdateReqVO.getId());
        MemberJobPageReqVO reqVO=new MemberJobPageReqVO();
        reqVO.setCompanyId(recruCompanyUpdateReqVO.getId());
        List<MemberJobDO> memberJobDos=memberJobMapper.selectList1(reqVO);
        if(recruCompanyUpdateReqVO.getStatus()!=null&&recruCompanyUpdateReqVO.getStatus().equals("-1")){//启用
            for (MemberJobDO memberJobDO:memberJobDos) {
                if(memberJobDO.getStatus().equals("3")){
                    memberJobDO.setStatus("2");
                    memberJobMapper.updateById(memberJobDO);
                }
            }
        }
        if(recruCompanyUpdateReqVO.getStatus()!=null&&recruCompanyUpdateReqVO.getStatus().equals("-2")){//禁用
            for (MemberJobDO memberJobDO:memberJobDos) {
                if(memberJobDO.getStatus().equals("2")) {
                    memberJobDO.setStatus("3");
                    memberJobMapper.updateById(memberJobDO);
                }
            }
        }
        // 更新
        CompanyDO updateObj =companyMapper.selectById(recruCompanyUpdateReqVO.getId());
        updateObj.setStatus(recruCompanyUpdateReqVO.getStatus());
        companyMapper.updateById(updateObj);
    }

    @Override
    public void setCompanyTop(RecruCompanyUpdateReqVO recruCompanyUpdateReqVO) {
        // 校验存在
        this.validateCompanyExists(recruCompanyUpdateReqVO.getId());
        // 更新
        CompanyDO updateObj =new CompanyDO();
        updateObj.setId(recruCompanyUpdateReqVO.getId());
        updateObj.setTopStatus(recruCompanyUpdateReqVO.getTopStatus());
        updateObj.setUpdater(SecurityFrameworkUtils.getLoginUser().getUsername());
        companyMapper.updateById(updateObj);
    }

    @Override
    public List<RecruitCompanyDO> getRecruitCompanyList(RecruitCompanyExportReqVO exportReqVO) {
        return recruitCompanyMapper.selectList1(exportReqVO);
    }

    @Override
    public CompanyDO getCompany(Long id) {
        CompanyDO companyDO=companyMapper.selectById(id);
        if(null!=companyDO){
            memberJobMapper.selectList("company_id",companyDO.getId());
        }
        return companyDO;
    }

    @Override
    public List<RecruitCompanyDO> getHotCompanyJobList(Date startDate, Date endDate) {
        return recruitCompanyMapper.getHotCompanyJobList(startDate,endDate);
    }

    @Override
    public CompanyInfoRespVO getCompanyInfoCount(Integer type, String startDate, String endDate) {
        if(type!=0){
            if(1==type){
                if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
                    throw new NullPointerException("开始结束时间不能为空");
                }
            }else if(2==type){
                startDate=DateUtils.getWeekStart(new Date());
                endDate=DateUtils.format(new Date());
            }else if(3==type){
                startDate=DateUtils.getMonthStart();
                endDate=DateUtils.format(new Date());
            }
            if(startDate.length()<12){
                startDate= startDate+" 00:00:00";
                endDate=endDate+" 23:59:59";
            }
        }
        CompanyInfoRespVO companyInfoRespVO=new CompanyInfoRespVO();
        List<RecruitCompanyDO> hotCompanyJobList = recruitCompanyMapper.getHotCompanyJobList1(startDate, endDate);
        companyInfoRespVO.setRecruitCompanyList(RecruitCompanyConvert.INSTANCE.convertList(hotCompanyJobList));
        CompanyCountVO companyCountVO=recruitCompanyMapper.getHotCompanyCount(startDate, endDate);
        companyInfoRespVO.setCompanyCountVO(companyCountVO);
        List<JobPassDo> feedBackDoList = recruitCompanyMapper.getFeedBackList(startDate,endDate);
        companyInfoRespVO.setJobPassList(RecruitCompanyConvert.INSTANCE.convertList1(feedBackDoList));
        JobAndInvitCountDo jobAndinvitCountNew = recruitCompanyMapper.getJobAndinvitCountNew(0, startDate, endDate);
        companyInfoRespVO.setJobAndInvitCountDo(jobAndinvitCountNew);
        List<DistributionDo> distributionDoList = recruitCompanyMapper.getCompanyDistribution(startDate, endDate);
        companyInfoRespVO.setDistributionList(distributionDoList);
        return companyInfoRespVO;
    }

    @Override
    public Distribution getStudentInfoCount(Integer type, String startDate, String endDate) {
        if(type!=0){
            if(1==type){
                if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
                    throw new NullPointerException("开始结束时间不能为空");
                }
            }else if(2==type){
                startDate=DateUtils.getWeekStart(new Date());
                endDate=DateUtils.format(new Date());
            }else if(3==type){
                startDate=DateUtils.getMonthStart();
                endDate=DateUtils.format(new Date());
            }
            if(startDate.length()<12){
                startDate= startDate+" 00:00:00";
                endDate=endDate+" 23:59:59";
            }
        }
        List<DistributionDo> jobDistribution=recruitCompanyMapper.getJobDistribution(startDate,endDate);
        List<DistributionDo> studentDistribution=recruitCompanyMapper.getStudentDistribution(startDate,endDate);
        Distribution distribution=new Distribution();
        distribution.setJobDistribution(jobDistribution);
        distribution.setStudentDistribution(studentDistribution);
        if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)){
            Long registerCount = studentMapper.selectCount(new QueryWrapper<StudentDO>().between("create_time",startDate,endDate));
            distribution.setRegisterStudentNum(registerCount);
            Long hotCount = studentMapper.selectCount(new QueryWrapper<StudentDO>().between( "login_date",startDate,endDate));
            distribution.setHotStudentNum(hotCount);
        }else{
            Long registerCount = studentMapper.selectCount(new QueryWrapper<StudentDO>());
            distribution.setRegisterStudentNum(registerCount);
            distribution.setHotStudentNum(registerCount);
        }


        Long sendStudentInfoCount = recruitCompanyMapper.getSendStudentInfoCount(startDate, endDate);
        distribution.setSendStudentNum(sendStudentInfoCount);
        return distribution;
    }

    @Override
    public JobAndInvitCountDo getJobAndinvitCount(Date startDate, Date endDate) {
        return recruitCompanyMapper.getJobAndinvitCount(startDate,endDate);
    }

    @Override
    public JobAndInvitCountDo getJobAndinvitCountNew(Integer type,Integer dayFlowFlag, String startDate, String endDate) {
        if(type!=0){
            if(1==type){
                if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
                    throw new NullPointerException("开始结束时间不能为空");
                }
            }else if(2==type){
                startDate=DateUtils.getWeekStart(new Date());
                endDate=DateUtils.format(new Date());
            }else if(3==type){
                startDate=DateUtils.getMonthStart();
                endDate=DateUtils.format(new Date());
            }
            if(startDate.length()<12){
                startDate= startDate+" 00:00:00";
                endDate=endDate+" 23:59:59";
            }
        }
        return recruitCompanyMapper.getJobAndinvitCountNew(dayFlowFlag,startDate,endDate);
    }

    @Override
    public List<JobPassDo> getFeedBackList(String startDate, String endDate) {
        return recruitCompanyMapper.getFeedBackList(startDate,endDate);
    }

    @Override
    public Distribution getDistribution(String startDate, String endDate) {
        Map<String,Object> map=new HashMap<>();
        List<DistributionDo> jobDistribution=recruitCompanyMapper.getJobDistribution(startDate,endDate);
        List<DistributionDo> studentDistribution=recruitCompanyMapper.getStudentDistribution(startDate,endDate);
        Distribution distribution=new Distribution();
        distribution.setJobDistribution(jobDistribution);
        distribution.setStudentDistribution(studentDistribution);
        return distribution;
    }

    @Override
    public Boolean isNoshowCompany(Long id) {
        CompanyDO companyDO=companyMapper.selectById(id);
        if(companyDO==null){
            throw exception(COMPANY_NOT_EXISTS);
        }
        if(StringUtils.isEmpty(companyDO.getCompanyShortName())){
            return true;
        }
        return false;
    }


    private void validateCompanyExists(Long id) {
        if (companyMapper.selectById(id) == null) {
            throw exception(RECRUIT_COMPANY_NOT_EXISTS);
        }
    }

    @Override
    public List<RecruitCompanyDO> getRankingCompanyList(RecruitCompanyPageReqVO request) {
        return recruitCompanyMapper.getRankingCompanyList(request);
    }

    @Override
    public void updateCompanyHot(Long id, Integer isHot) {
        recruitCompanyMapper.updateCompanyHot(id,isHot);
    }

    @Override
    public void updateCompanyNew(Long id, Integer isNew) {
        recruitCompanyMapper.updateCompanyNew(id,isNew);
    }

    @Override
    public void updateRankingCompanySort(RankingCompanyIdReqVO rankingIdReqVO) {
        List<Long> ids = rankingIdReqVO.getIds();
        int size=ids.size();
        List<CompanyDO> list=new ArrayList<>();
        for (int i = 0; i <size ; i++) {
            CompanyDO r=new CompanyDO();
            r.setId(ids.get(i));
            if(1==rankingIdReqVO.getType()){
                r.setHotWeight(size-i);
            }else if(2==rankingIdReqVO.getType()){
                r.setNewWeight(size-i);
            }
            list.add(r);
        }
        if(1==rankingIdReqVO.getType()){
            recruitCompanyMapper.rankingHotCompanySort(list);
        }else if(2==rankingIdReqVO.getType()){
            recruitCompanyMapper.rankingNewCompanySort(list);
        }

    }
}
